package com.foreveross.chameleon.task;

import android.content.Context;

import com.foreveross.chameleon.config.ChameleonConstants;
import com.foreveross.chameleon.manager.BroadcastManager;
import com.foreveross.chameleon.utils.FileCopeTool;
import com.foreveross.chameleon.utils.ZipUtils;

/**
 * @description unzip assets/www.zip to sd card 
 * @author zhouzhineng
 *
 */
public class UnzipWwwAsyncTask extends BaseAsyncTask{
	protected Context mContext;

	public UnzipWwwAsyncTask(Context context) {
		this.mContext = context;
	}

	@Override
	protected void onPreExecute() {
		System.out.println(System.currentTimeMillis() + "");
	}

	@Override
	protected void onPostExecute(Boolean result) {
		BroadcastManager.getInstance().send(ChameleonConstants.ACTION_APP_INIT_FINISH);
		if (result) {
			System.out.println(System.currentTimeMillis() + "");
			BroadcastManager.getInstance().send(ChameleonConstants.ACTION_APP_INIT_SUCC);
		} else {
			BroadcastManager.getInstance().send(ChameleonConstants.ACTION_APP_INIT_FAIL);
		}
	}

	@Override
	protected Boolean doInBackground(String... params) {
		boolean result = false;
		BroadcastManager.getInstance().send(ChameleonConstants.ACTION_APP_INIT_START);
		try {
			FileCopeTool tool = new FileCopeTool(mContext);
			boolean copySuccess = tool.CopyAssetsFile(params[0], params[1]);
			if (copySuccess) {
				if (ZipUtils.unZipFile(params[1], params[2])) {
					result = true;
				}
			}
		} catch (Exception e) {
			result = true;
			e.printStackTrace();
		}
		
		return result;
	}
}
